//
//  MyWallet.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/23.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "MyWallet.h"
#import "ReChargeViewController.h"
#import "WithdrawViewController.h"
#import "LiQuanCollectionViewCell.h"
#import "TicketModel.h"
@interface MyWallet ()
{
    NSInteger page;
}

@end

@implementation MyWallet
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    [self getMyMoney];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的钱包";
    page =1;
    [self requestData];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing =9*kWidthScale;
    layout.itemSize = CGSizeMake((kScreenWidth-27*kWidthScale)/2, 134);
    layout.sectionInset = UIEdgeInsetsMake(9, 9*kWidthScale, 9, 9*kWidthScale);
    //[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 391*kHeightScale) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [self.baseView addSubview:self.collectionView];
     [self.collectionView registerNib:[UINib nibWithNibName:@"LiQuanCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"liquanCell"];
}
- (void)requestData
{
    NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, kTicketsList];
    NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"page":[NSNumber numberWithInteger:page]};
    [NetworkHandle getUpDataWithUrl:string class:NSClassFromString(@"TicketModel") dictionary:dict firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:array];
        if (self.dataArray.count >= 10 && self.collectionView.footer == nil) {
            self.collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        [self.collectionView.footer endRefreshing];
        [self.collectionView.header endRefreshing];
        [self.collectionView reloadData];
    }];
        
  
}
- (void)headerRefresh
{
    page = 1;
    [self requestData];
}

- (void)footerRefresh
{
    page++;
    [self requestData];
}
- (void)getMyMoney
{
    // /user.do?cmd=my_wallet&u=X
    NSString *url = [NSString stringWithFormat:@"%@%@", kBaseUrl, @"/user.do?cmd=my_wallet"];
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]};
    [NetworkHandle getInfoWithUrl:url dictionary:dic success:^(NSDictionary *dictionary) {
        self.MyMoneyLabel.text =dictionary[@"data"];
    }];
    
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LiQuanCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"liquanCell" forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.row];
    return cell;
}


#pragma mark -- 按钮点击事件
// 充值
- (IBAction)actionRechargeBtn:(id)sender {
    ReChargeViewController *rechargeVC = [[ReChargeViewController alloc] initWithNibName:@"ReChargeViewController" bundle:nil];
    [self.navigationController pushViewController:rechargeVC animated:YES];
    
    
}

// 提现
- (IBAction)actionWithdraw:(id)sender {
    WithdrawViewController *withdrawVC = [[WithdrawViewController alloc] initWithNibName:@"WithdrawViewController" bundle:nil];
    withdrawVC.money = self.MyMoneyLabel.text;
    [self.navigationController pushViewController:withdrawVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
